package com.huadao.pda.project.bindingProject;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.huadao.pda.R;
import com.huadao.pda.base.BasePresenter;
import com.huadao.pda.base.BaseScanDialogFragment;
import com.huadao.pda.common.GsonUtil;
import com.huadao.pda.data.bean.DeviceInfo;
import com.huadao.pda.data.bean.Project;

import butterknife.BindView;
import butterknife.OnClick;

public class BindingProjectDF extends BaseScanDialogFragment implements IBindingProjectContract.IBindingProjectView {

    @BindView(R.id.tv_deviceName)
    TextView tvDeviceName;
    @BindView(R.id.tv_deviceNumber)
    TextView tvDeviceNumber;
    @BindView(R.id.tv_MacAddress)
    TextView tvMacAddress;
    @BindView(R.id.tv_ip)
    TextView tvIp;
    @BindView(R.id.tv_port)
    TextView tvPort;
    @BindView(R.id.btn_cancel)
    Button btnCancel;
    @BindView(R.id.btn_confirm_bind)
    Button btnConfirmBind;
    private Project project;

    private BindingProjectPresenter presenter;
    private DeviceInfo CART_deviceInfo;

    private OnBind onBind;
    public interface OnBind{
        void onbindSucc(Project project,DeviceInfo deviceInfo);
    }

    public static BindingProjectDF newInstance(DeviceInfo deviceInfo,Project project) {

        Bundle args = new Bundle();
        args.putSerializable("project", project);
        args.putSerializable("deviceInfo", deviceInfo);

        BindingProjectDF fragment = new BindingProjectDF();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected BasePresenter getPresenter() {
        return presenter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfragment_binding_project;
    }

    @Override
    protected void onInit() {
        presenter=new BindingProjectPresenter(this);
        Bundle bundle = getArguments();
        if (bundle != null) {
            project = (Project) bundle.get("project");
            CART_deviceInfo= (DeviceInfo) bundle.get("deviceInfo");
        }
        if (project != null) {
            initProjectData();
        }
        if(CART_deviceInfo!=null){
            initDeviceData();
        }
        setTitle(getString(R.string.binding));
        setBackVisibility(View.VISIBLE);

//        ScanDF scanDF=new ScanDF();
//        scanDF.show(getFragmentManager(),null);
//        showToast("请使用PDA扫码");
    }

    private void initProjectData() {

    }

    private void initDeviceData() {
        if(CART_deviceInfo==null){
            return;
        }
        if(!TextUtils.isEmpty(CART_deviceInfo.getDeviceName())) {
            tvDeviceName.setText(CART_deviceInfo.getDeviceName());
        }else {
            tvDeviceName.setText(R.string.noValue);
        }
        if(!TextUtils.isEmpty(CART_deviceInfo.getDeviceId())) {
            tvDeviceNumber.setText(CART_deviceInfo.getDeviceId());
        }else {
            tvDeviceNumber.setText(R.string.noValue);
        }
        if(!TextUtils.isEmpty(CART_deviceInfo.getMacAddress())) {
            tvMacAddress.setText(CART_deviceInfo.getMacAddress());
        }else {
            tvMacAddress.setText(R.string.noValue);
        }
        if(!TextUtils.isEmpty(CART_deviceInfo.getIpAddress())) {
            tvIp.setText(CART_deviceInfo.getIpAddress());
        }else {
            tvIp.setText(R.string.noValue);
        }
        if(!TextUtils.isEmpty(CART_deviceInfo.getProt())) {
            tvPort.setText(CART_deviceInfo.getProt());
        }else {
            tvPort.setText(R.string.noValue);
        }
    }

    @Override
    public void onScanResult(String barcode) {
        //{"deviceType":"CART-CCS","id":"3649410996423884800","macAddress":"12:a4:be:a8:7b:83","status":0,"type":0,"code":0}
        Log.i(TAG,barcode);
        stopScan();
        try {
            CART_deviceInfo=GsonUtil.getInstance().getGson().fromJson(barcode,DeviceInfo.class);
        }catch (Exception e){
            e.printStackTrace();
            showToast("信息有误");
        }
        initDeviceData();
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm_bind})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            case R.id.btn_confirm_bind:
//                startScan();
                if(CART_deviceInfo==null){
                    showToast("未获得CART-CCS设备信息");
                    return;
                }
                presenter.bindProject(project.getId(),CART_deviceInfo.getId());
                break;
        }
    }

    public void setOnBind(OnBind onBind) {
        this.onBind = onBind;
    }

    @Override
    public void bindProjectSucc() {
        showToast("绑定成功");
        if(onBind!=null){
            onBind.onbindSucc(project,CART_deviceInfo);
        }
        dismiss();
    }

    @Override
    public void bindProjectFail() {
        showToast("绑定失败");
    }
}
